package lib

import (
	"fmt"
)

// 类似包的默认构造函数
func init() {
	fmt.Println("初始化")
}

func Test1() {
	fmt.Println("测试1")
}

func sum(datas [5]int) int {
	sum := 0
	for _, val := range datas {
		sum += val
	}
	return sum
}
func Test2() {
	var datas [5]int
	for i := 0; i < 5; i++ {
		datas[i] = i
	}
	fmt.Println(sum(datas))
}

//切片

func Test3(datas []string) string {
	res := string("[data]: ")
	for _, val := range datas {
		res += val
	}
	return res
}

//指针

func Swap(a *int, b *int) {
	fmt.Printf("a = %d,b = %d\n", *a, *b)

	tmp := 0
	tmp = *a
	*a = *b
	*b = tmp
	fmt.Printf("a = %d,b = %d\n", *a, *b)
}

//Map

func Insert_to_map(src *map[string]any, key string, vals ...any) {
	if src == nil {
		newmap := make(map[string]any)
		src = &newmap
	}
	//any表示可以是任何类型

	for i, val := range vals {
		var tmpk string = key
		tmpk += "00"
		tmpk += fmt.Sprintf("%d", i)
		(*src)[tmpk] = val
	}
}

type Status struct {
	level int
}

// 组合 优于 继承
type Person struct {
	st    Status
	name  string
	age   int
	score float64
	total float64
}

func (this *Person) Name() string {
	return this.name
}
func (this *Person) Age() int {
	return this.age
}
func MakePerson(level int, name string, age int, score, total float64) Person {
	tmpst := Status{level}
	return Person{
		st:    tmpst,
		name:  name,
		age:   age,
		score: score,
		total: total,
	}
}

type Base interface {
	Eat()
	Sleep()
}

type Animal struct {
}

func (this *Animal) Eat() {
	fmt.Println("猫叫")
}
